﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IssueIT.WebUI.Models;
using IssueIT.Core.Domain.Model;
using IssueIT.Core.Services;

namespace IssueIT.WebUI.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        private readonly IProjectService _projectService;

        public AdminController(IProjectService projectService)
        {
            _projectService = projectService;
        }

        
        public ActionResult AddProject()
        {
            GetAddProjectViewBag();
            return View();
        }

        private void GetAddProjectViewBag()
        {
            ViewBag.Title = "Add new project";
            ViewBag.Heading = "Add new project";
        }

        [HttpPost]
        public ActionResult AddProject(NewProjectPostModel newProject)
        {
            if (!ModelState.IsValid)
            {
                GetAddProjectViewBag();
                return View();
            }

            Project project = new Project(newProject.Title, IssueIT.Core.Domain.Model.User.Unknown ){UriName = newProject.ProjectUri};

            _projectService.CreateProject(project);

            return new RedirectToRouteResult("ProjectList", new RouteValueDictionary() {{ "project", project.UriName} }); 
        }

        public ActionResult ValidateProjectUri(string suggestedUri)
        {
            return Json(_projectService.IsUnique(suggestedUri), JsonRequestBehavior.DenyGet);
        }
    }
}
